var ANDROID = 1;
var IOS = 2;
var curApp = "";
var ua = window.navigator.userAgent.toLowerCase();
if (/android/.test(ua)) {
	curApp = ANDROID;
} else if (/(iphone|ipad|ipod|ios)/.test(ua)) {
	curApp = IOS;
} else {
	document.write("curApp =>  其他类型 => " + ua);
}
var isWallet = false;
if (ua.indexOf('wallet') > 0) {
	isWallet = true;
}
if (curApp === ANDROID) {
	var bridge = {
		//default:this,
		call: function(b, a, c) {
			var e = "";
			"function" == typeof a && ((c = a), (a = {}));
			a = {
				data: void 0 === a ? null : a,
			};
			if ("function" == typeof c) {
				var g = "dscb" + window.dscb++;
				window[g] = c;
				a._dscbstub = g;
			}
			a = JSON.stringify(a);
			if (window._dsbridge) e = _dsbridge.call(b, a);
			else if (window._dswk || -1 != navigator.userAgent.indexOf("_dsbridge"))
				e = prompt("_dsbridge=" + b, a);
			return JSON.parse(e || "{}").data;
		},
		register: function(b, a, c) {
			c = c ? window._dsaf : window._dsf;
			window._dsInit ||
				((window._dsInit = !0),
					setTimeout(function() {
						bridge.call("_dsb.dsinit");
					}, 0));
			"object" == typeof a ? (c._obs[b] = a) : (c[b] = a);
		},
		registerAsyn: function(b, a) {
			this.register(b, a, !0);
		},
		hasNativeMethod: function(b, a) {
			return this.call("_dsb.hasNativeMethod", {
				name: b,
				type: a || "all",
			});
		},
		disableJavascriptDialogBlock: function(b) {
			this.call("_dsb.disableJavascriptDialogBlock", {
				disable: !1 !== b,
			});
		},
	};
	!(function() {
		//!function "执行"
		if (!window._dsf) {
			var b = {
					_dsf: {
						_obs: {},
					},
					_dsaf: {
						_obs: {},
					},
					dscb: 0,
					dsBridge: bridge,
					close: function() {
						bridge.call("_dsb.closePage");
					},
					_handleMessageFromNative: function(a) {
						var e = JSON.parse(a.data),
							b = {
								id: a.callbackId,
								complete: !0,
							},
							c = this._dsf[a.method],
							d = this._dsaf[a.method],
							h = function(a, c) {
								b.data = a.apply(c, e);
								bridge.call("_dsb.returnValue", b);
							},
							k = function(a, c) {
								e.push(function(a, c) {
									b.data = a;
									b.complete = !1 !== c;
									bridge.call("_dsb.returnValue", b);
								});
								a.apply(c, e);
							};
						if (c) h(c, this._dsf);
						else if (d) k(d, this._dsaf);
						else if (((c = a.method.split(".")), !(2 > c.length))) {
							a = c.pop();
							var c = c.join("."),
								d = this._dsf._obs,
								d = d[c] || {},
								f = d[a];
							f && "function" == typeof f ?
								h(f, d) :
								((d = this._dsaf._obs),
									(d = d[c] || {}),
									(f = d[a]) && "function" == typeof f && k(f, d));
						}
					},
				},
				a;
			for (a in b) window[a] = b[a];
			bridge.register("_hasJavascriptMethod", function(a, b) {
				b = a.split(".");
				if (2 > b.length) return !(!_dsf[b] && !_dsaf[b]);
				a = b.pop();
				b = b.join(".");
				return (b = _dsf._obs[b] || _dsaf._obs[b]) && !!b[a];
			});
		}
	})();
} else if (curApp == IOS) {
	var jsBridge = function(bridgeName, bridgeScheme) {
		this.bridgeName = bridgeName || "WebViewJavascriptBridge";
		this.bridgeScheme = bridgeScheme || "https";
		var ua = navigator.userAgent || navigator.vendor || window.opera;
		var ANDROID = 1;
		var IOS = 2;
		var SUCCESS_CODE = 0;
		ua = ua.toLowerCase();
		if (/android/.test(ua)) {
			this.app = ANDROID;
		} else if (/(iphone|ipad|ipod|ios)/.test(ua)) {
			this.app = IOS;
		}

		/* 获取 bridge */
		this.getBridge = function(callback) {
			if (window[this.bridgeName]) {
				callback(window[this.bridgeName]);
				return;
			}
			if (this.app === ANDROID) {
				document.addEventListener(
					"WebViewJavascriptBridgeReady",
					function() {
						callback(window[this.bridgeName]);
					},
					false
				);
			} else if (this.app === IOS) {
				// WebViewJavascriptBridge 由native在注入
				// https://github.com/marcuswestin/WebViewJavascriptBridge
				if (window.WVJBCallbacks) {
					return window.WVJBCallbacks.push(callback);
				}
				window.WVJBCallbacks = [callback];
				var WVJBIframe = document.createElement("iframe");
				WVJBIframe.style.display = "none";
				WVJBIframe.src = "https://__bridge_loaded__";
				document.documentElement.appendChild(WVJBIframe);
				setTimeout(function() {
					document.documentElement.removeChild(WVJBIframe);
				}, 0);
			}
		};
		/**
		 * 给ios和android注册事件监听
		 *
		 * @memberof jsBridge
		 */
		this.on = function(event, jsHandler) {
			this.registHandler(event, jsHandler);
		};

		/**
		 * 默认监听事件
		 *
		 * @memberof jsBridge
		 */
		this.registDefaultHandlerForApp = function() {
			this.on("reload", function() {
				location.reload();
			});
		};

		/**
		 * 注册事件逻辑
		 *
		 * @memberof jsBridge
		 */
		this.registHandler = function(event, jsHandler) {
			this.getBridge(function(bridge) {
				if (
					bridge &&
					bridge.registerHandler &&
					typeof bridge.registerHandler === "function"
				) {
					bridge.registerHandler(event, function(data2js, responseCallback) {
						responseCallback = responseCallback || function() {};
						jsHandler(data2js, responseCallback);
					});
				}
			});
		};
		/**
		 * 调用 app
		 * @param {string} event
		 * @param {object} params
		 * @param {fun} callback
		 * @return JSON对象
		 */
		this.callHandler = function(event, params, callback) {
			this.getBridge(function(bridge) {
				if (
					bridge &&
					bridge.registerHandler &&
					typeof bridge.registerHandler === "function"
				) {
					bridge.callHandler(event, params, callback);
				}
			});
		};
		this.registDefaultHandlerForApp();

		return this;
	};
}

/**
 * 扫码
 */
function scanQRCode(params, success) {
	if (curApp == ANDROID) {
		dsBridge.call("scanQRCode", params, success);
	} else if (curApp == IOS) {
		jsBridge.callHandler("scanQRCode", params, success);
	}
};
/**
 * 获取BTY地址
 */
export function getCurrentBTYAddress(params, success) {
	if (curApp == ANDROID) {
		dsBridge.call("getCurrentBTYAddress", params, success);
	} else if (curApp == IOS) {
		jsBridge.callHandler("getCurrentBTYAddress", params, success);
	}
};
/**
 *  获取所有币种的地址：getAddress
 *  入参：cointype为BTC,ETH,TRX,BTY等主链
 */
export function getAddress(params, success) {
	if (curApp == ANDROID) {
		dsBridge.call("getAddress", params, success);
	} else if (curApp == IOS) {
		jsBridge.callHandler("getAddress", params, success);
	}
};
/**
 * 设置标题
 */
export function setTitle(params, success) {
	if (curApp == ANDROID) {
		dsBridge.call("setTitle", params, success);
	} else if (curApp == IOS) {
		jsBridge.callHandler("setTitle", params, success);
	}
};
/**
 * 获取当前语言
 */
export function getLang(params, success) {
	if (curApp == ANDROID) {
		dsBridge.call("getLang", params, success);
	} else if (curApp == IOS) {
		jsBridge.callHandler("getLang", params, success);
	}
};
/**
 * 返回上一个网页
 */
export function closeCurrentWebview(params, success) {
	if (curApp == ANDROID) {
		dsBridge.call("closeCurrentWebview", params, success);
	} else if (curApp == IOS) {
		jsBridge.callHandler("closeCurrentWebview", params, success);
	}
};
/**
 *  获取设备id：getDeviceId
 */
export function getDeviceId(params, success) {
	if (curApp == ANDROID) {
		dsBridge.call("getDeviceId", params, success);
	} else if (curApp == IOS) {
		jsBridge.callHandler("getDeviceId", params, success);
	}
};
/**
 *  跳转钱包 gotoWallet
 */
export function gotoWallet(params, success) {
	if (curApp == ANDROID) {
		dsBridge.call("gotoWallet", params, success);
	} else if (curApp == IOS) {
		jsBridge.callHandler("gotoWallet", params, success);
	}
};
/**
 * 微信分享
 */
export function wechatShare(params, success) {
	if (curApp == ANDROID) {
		dsBridge.call("wechatShare", params, success);
	} else if (curApp == IOS) {
		jsBridge.callHandler("wechatShare", params, success);
	}
};
/**
 * 构造数据：create
 */
export function create(params, success) {
	if (curApp == ANDROID) {
		dsBridge.call("create", params, success);
	} else if (curApp == IOS) {
		jsBridge.callHandler("create", params, success);
	}
};
/**
 *  普通签名：sign
 */
export function sign(params, success) {
	if (curApp == ANDROID) {
		dsBridge.call("sign", params, success);
	} else if (curApp == IOS) {
		jsBridge.callHandler("sign", params, success);
	}
};
/**
 *  交易组签名：signTxGroup
 *  入参：交易组只支持BTY
 */
function signTxGroup(params, success) {
	if (curApp == ANDROID) {
		dsBridge.call("signTxGroup", params, success);
	} else if (curApp == IOS) {
		jsBridge.callHandler("signTxGroup", params, success);
	}
};
/**
 *  发送交易：send
 *  cointype为BTC,ETH,TRX,BTY等主链
 */
export function send(params, success) {
	if (curApp == ANDROID) {
		dsBridge.call("send", params, success);
	} else if (curApp == IOS) {
		jsBridge.callHandler("send", params, success);
	}
};

if (!jsBridge) {
	jsBridge = function() {};
}
//jsBridge.prototype.curApp = curApp;
//export default new jsBridge();
var jsBridge = new jsBridge();
export default {
	curApp,
	isWallet,
	jsBridge,
	scanQRCode,
	getCurrentBTYAddress,
	getAddress,
	setTitle,
	getLang,
	closeCurrentWebview,
	getDeviceId,
	gotoWallet,
	wechatShare,
	create,
	sign,
	signTxGroup,
	send
}